﻿namespace NetDon.OA.Portal.Admin
{
    using System;
    using System.Data;
    using Data.CollectionClasses;
    using Data.DaoClasses;
    using Data.HelperClasses; 
    using SD.LLBLGen.Pro.ORMSupportClasses;
    using Utilities;
    using NetDon.OA.Interface;

    public partial class EventBulkDelete : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ddlApplication.InsertEmptyItem(ListEmptyType.All);
                LoadCategories();
                CancelButton = btnCancel;
            }
        }

        private void LoadCategories()
        {
            var table = new DataTable();
            var fields = new EntityFields(1);
            fields[0] = LogFields.Category;
            var dao = new TypedListDAO();
            dao.GetMultiAsDataTable(fields, table, 0, new SortExpression(LogFields.Category | SortOperator.Ascending),
                                    null, null, false, null, null, 0, 0);
            ddlCategory.AppendDataBoundItems = true;
            ddlCategory.DataTextField = LogFields.Category.Name;
            ddlCategory.DataValueField = LogFields.Category.Name;
            ddlCategory.DataSource = table;
            ddlCategory.DataBind();
            ddlCategory.InsertEmptyItem(ListEmptyType.All);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            var predicate = new PredicateExpression();
            if (!string.IsNullOrEmpty(txtStartDate.Text.Trim()))
            {
                predicate &= LogFields.LogTime > DateTime.Parse(txtStartDate.Text);
            }
            if (!string.IsNullOrEmpty(txtEndDate.Text.Trim()))
            {
                predicate &= LogFields.LogTime < DateTime.Parse(txtEndDate.Text).AddDays(1);
            }
            //if (!string.IsNullOrEmpty(ddlApplication.SelectedValue))
            //{
            //    predicate &= LogFields.ApplicationName == ddlApplication.SelectedValue;
            //}
            if (!string.IsNullOrEmpty(ddlLogLevel.SelectedValue))
            {
                predicate &= LogFields.Level == (int) ddlLogLevel.SelectedValue.ToEnum<LogLevel>();
            }
            if (!string.IsNullOrEmpty(ddlCategory.SelectedValue))
            {
                predicate &= LogFields.Category == ddlCategory.SelectedValue;
            }
            var collection = new LogCollection();
            int count = collection.DeleteMulti(predicate);
            Success(count + " 笔记录被删除。");
            //Success(count + GetResourceString("recorddelete"));
            CloseWindow(count > 0);
        }
    }
}